//
//  DCIMLoginViewController.m
//  xmppBase
//
//  Created by striby on 16/1/7.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCIMLoginViewController.h"
#import "DCIMRegisterViewController.h"


@interface DCIMLoginViewController ()


@property(nonatomic,strong)RCAnimatedImagesView * animatedImagesView;

@end

@implementation DCIMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.animatedImagesView = [[RCAnimatedImagesView alloc]
                               initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,
                                                        self.view.bounds.size.height)];
    [self.view addSubview:self.animatedImagesView];
    self.animatedImagesView.delegate = self;

    
    
    UIColor *color = [UIColor whiteColor];

    
    UIButton * registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(DCDeviceWidth-80, 20, 60, 40)];
    [registerBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(gotoRegister) forControlEvents:UIControlEventTouchUpInside];
    [registerBtn setTitle:@"新用户" forState:UIControlStateNormal];
    [self.view addSubview:registerBtn];
    
    
    self.userName = [[RCUnderlineTextField alloc]initWithFrame:CGRectMake(40, 80, DCDeviceWidth-80, 40)];
    self.userName.backgroundColor = [UIColor clearColor];
    self.userName.borderStyle = UITextBorderStyleNone;
    self.userName.attributedPlaceholder = [[NSAttributedString alloc]
                                               initWithString:@"账号"
                                               attributes:@{NSForegroundColorAttributeName : color}];
    
    self.userName.textColor = color;
    [self.view addSubview:self.userName];
    
    
    self.password = [[RCUnderlineTextField alloc]initWithFrame:CGRectMake(40, 140, DCDeviceWidth-80, 40)];
    self.password.backgroundColor = [UIColor clearColor];
     self.password.borderStyle = UITextBorderStyleNone;
    
    self.password.attributedPlaceholder = [[NSAttributedString alloc]
                                           initWithString:@"密码"
                                           attributes:@{NSForegroundColorAttributeName : color}];
    
    self.password.textColor = color;
    [self.view addSubview:self.password];
    
    self.regAndLogin = [[UIButton alloc]initWithFrame:CGRectMake(40, 210, DCDeviceWidth-80, 45)];
    [self.regAndLogin setTitle:@"登录" forState:UIControlStateNormal];
    [self.regAndLogin setTitleColor:color forState:UIControlStateNormal];
    [self.regAndLogin setBackgroundImage:[UIImage imageNamed:@"login_button"] forState:UIControlStateNormal];
 
  
    
    [self.view addSubview:self.regAndLogin];
    
    
      [self.regAndLogin addTarget:self action:@selector(regiserAndLogin) forControlEvents:UIControlEventTouchUpInside];
    
    NSUserDefaults * user =[NSUserDefaults standardUserDefaults];
    
    
    if ([user objectForKey:kMyJID]) {
        self.userName.text = [user objectForKey:kMyJID];
    }
    
    if ([user objectForKey:kPS]) {
        self.password.text = [user objectForKey:kPS];
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.animatedImagesView startAnimating];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [self.animatedImagesView stopAnimating];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}



#pragma mark - animationView
- (NSUInteger)animatedImagesNumberOfImages:
(RCAnimatedImagesView *)animatedImagesView {
    return 2;
}

- (UIImage *)animatedImagesView:(RCAnimatedImagesView *)animatedImagesView
                   imageAtIndex:(NSUInteger)index {
    return [UIImage imageNamed:@"animationBackground.jpg"];
    
}

#pragma mark - private method

-(void)gotoRegister
{
    DCIMRegisterViewController * vc =[[DCIMRegisterViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
}

-(BOOL)checkInfo
{
    if (self.userName.text.length>0&&self.password.text.length>0) {
        return YES;
    }
    
    
    return NO;
}

-(void)regiserAndLogin
{
    [[DCXMPPTool shareInstance] logoutXmppHost];
    
    if (![self checkInfo]) {
        if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil];
            
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请填写完整信息" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
    else
    {
        //        [self registerUser];
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        
        [defaults setObject:self.userName.text forKey:kMyJID];
        [defaults setObject:self.password.text forKey:kPS];
        
    
        
        [DCXMPPTool shareInstance].connectionStatus=XmppConnectionStatusLogin;
        
        [[DCXMPPTool shareInstance] connectXmppHost];
    }
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
